import huck
import huck.api.keyvalue
import memcache

class KeyValue(huck.api.keyvalue.Abstract):

  def __init__(self, handler, type, resource):
    super(KeyValue, self).__init__(handler, type)
    self.__resource = resource
    self.__client = huck.api.resource[self.__resource]

  def add(self, key, value, time=0):
    self.__client.add(key, value, time)

  def delete(self, key):
    self.__client.delete(key)

  def get(self, key):
    return self.__client.get(key)

  def replace(self, key, value, time=0):
    self.__client.replace(key, value, time)

  def set(self, key, value, time=0):
    self.__client.set(key, value, time)

  def multi_add(self, mapping, time=0):
    self.__client.add_multi(mapping, time)

  def multi_delete(self, keys):
    self.__client.delete_multi(keys)

  def multi_get(self, keys):
    return self.__client.get_multi(keys)

  def multi_replace(self, mapping, time=0):
    self.__client.replace_multi(mapping, time)

  def multi_set(self, mapping, time=0):
    self.__client.set_multi(mapping, time)

  def decr(self, key, delta=1, initial_value=None):
    return self.__client.decr(key, delta, initial_value)

  def incr(self, key, delta=1, initial_value=None):
    return self.__client.incr(key, delta, initial_value)

class Create(object):

  def __init__(self, type='default', resource='memcache'):
    self.type = type
    self.resource = resource

  def setup(self, application):
    huck.api.keyvalue.set(KeyValue, self.type, self.resource)
    return application
